/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skycode.segu.mbean;

import com.skycode.tuto.dto.MonitorDto;
import com.skycode.tuto.dto.MonitorPdfDto;
import com.skycode.tuto.dto.MonitorVideoDto;
import com.skycode.tuto.dto.PdfCommentDto;
import com.skycode.tuto.dto.VideoCommentDto;
import com.skycode.tuto.manager.MonitorManager;
import com.skycode.tuto.manager.MonitorPdfManager;
import com.skycode.tuto.manager.MonitorVideoManager;
import com.skycode.tuto.manager.PdfCommentManager;
import com.skycode.tuto.manager.VideoCommentManager;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@SessionScoped
@ManagedBean
public class MonitorMBean implements Serializable {

    private MonitorDto dto;
    private MonitorDto dtoFiltro;
    private List<MonitorDto> lista;
    private MonitorManager monitorManager;
    private UploadedFile upFile;
    private MonitorVideoDto dtoVideo;
    private MonitorPdfDto dtoPdf;
    private MonitorVideoManager monitorVideoManager;
    private MonitorPdfManager monitorPdfManager;
    private VideoCommentDto commentVDto;
    private PdfCommentDto commentPDto;
    private VideoCommentManager vCommentManager;
    private PdfCommentManager pCommentManager;
    private int accion;
    private int accionDetalle;
    private int verVideo;
    private int verPdf;    
    private boolean esAdmin;
    private String urlImagen;
    private Long idComentarioAEliminar;

    public MonitorMBean() {
        monitorManager = new MonitorManager();
        dto = monitorManager.inicializar();
        dtoFiltro = monitorManager.inicializar();
        lista = null;
        monitorVideoManager = new MonitorVideoManager();
        monitorPdfManager = new MonitorPdfManager();
        vCommentManager = new VideoCommentManager();
        pCommentManager = new PdfCommentManager();
        this.accion = -1;
        this.accionDetalle = -1;
        this.verVideo = 0;
        this.verPdf = 0;
        this.esAdmin = false;
        this.urlImagen = "";
        commentVDto = vCommentManager.inicializar();
        commentPDto = pCommentManager.inicializar();
        this.idComentarioAEliminar = 0L;
    }

    public String iniciar() {
        this.dtoFiltro = this.monitorManager.inicializar();
        this.lista = new ArrayList<MonitorDto>();
        return this.monitorManager.getUrlLista();
    }

    public void buscar() {
        this.lista = this.monitorManager.obtenerMonitores(dtoFiltro);
    }

    public String nuevo() {   
        this.accion = 0;
        this.dto = this.monitorManager.inicializar();
        return this.monitorManager.getUrl();
    }
    
    public void nuevoVideo() {
        this.accionDetalle = 0;
        this.dtoVideo = this.monitorVideoManager.inicializar();
        this.dtoVideo.setMonitorDto(this.dto);
    }
    
    public void nuevoPdf() {
        this.accionDetalle = 0;
        this.dtoPdf = this.monitorPdfManager.inicializar();
        this.dtoPdf.setMonitorDto(this.dto);
    }
    
    public String ver() {
        this.accion = 1;
        return this.monitorManager.getUrl();
    }

    public void fileUploadListener(FileUploadEvent event) {
        this.upFile = event.getFile();
        try {
            String nombre = event.getFile().getFileName();
            String extension = obtenerExtension(nombre);
            System.out.println("EXTENSION: " + extension);
            if (extension.equals(".avi")) {
                OutputStream out = new FileOutputStream("D:/PruebaLA/LazarteTuto111/web/videos/" + event.getFile().getFileName());
                out.write(event.getFile().getContents());
                out.close();
                this.dtoVideo.setNombre(event.getFile().getFileName());
                this.dtoVideo.setUrlVideo("/videos/" + event.getFile().getFileName());
            } else if (extension.equals(".pdf")) {
                OutputStream out = new FileOutputStream("D:/PruebaLA/LazarteTuto111/web/pdf/" + event.getFile().getFileName());
                out.write(event.getFile().getContents());
                out.close();
                this.dtoPdf.setNombre(event.getFile().getFileName());
                this.dtoPdf.setUrlPdf("/pdf/" + event.getFile().getFileName());
            }
        } catch (Exception e) {
            System.out.println("EXCEPTION!!!");
            e.printStackTrace();
        }
    }
    
    public void guardarVideo() {
        this.accionDetalle = 1;
        this.monitorVideoManager.guardar(this.dtoVideo);
        this.dto = this.monitorManager.obtenerPorId(this.dto.getId());
    }
    
    public void verVideo() {
        System.out.println("VIENDO VIDEO");
        System.out.println("TAMAÑO DE LA LISTA DE VIDEOS: " + this.dtoVideo.getListaComentariosVideo().size());
        this.dtoVideo.setListaComentariosVideo(this.vCommentManager.obtenerTodosPorId(this.dtoVideo.getId()));
        this.verVideo = 1;
    }
    
    public void descargarVideo() {
        //D:\PruebaLA\LazarteTuto111\web\videos
        String url = "http://localhost:8989/LazarteTuto111/videos/" + this.dtoVideo.getNombre();
        System.out.println("URL: " + url);
        String nombreArchivo = this.dtoVideo.getNombre();
        File dir = new File("D:/");
        File file = new File(dir + nombreArchivo);
        try {
            URLConnection cnn = new URL(url).openConnection();
            cnn.connect();
            InputStream in = cnn.getInputStream();
            OutputStream out = new FileOutputStream(file);
            
            int b = 0;
            while (b != -1) {
                b = in.read();
                if (b != -1) {
                    out.write(b);
                }
            }
            System.out.println("RUTA DEL ARCHIVO: " + file.getAbsolutePath());
            in.close();
            out.close();
            System.out.println("FIN DE LA DESCARGA");
            //Runtime.getRuntime().exec("C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe " + this.dtoVideo.getNombre());
            //Desktop.getDesktop().open(file);
            Process p = Runtime.getRuntime().exec ("rundll32 SHELL32.DLL,ShellExec_RunDLL "+"D:\\"+this.dtoVideo.getNombre());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void verPdf() {
        System.out.println("VIENDO PDF");        
        this.verPdf = 1;
    }
    
    public void cerrar() {
        this.accionDetalle = -1;
    }
    
    public void cerrarVideo() {
        System.out.println("CERRANDO VIDEO");
        this.verVideo = 0;
    }
    
    public void cerrarPdf() {
        this.verPdf = 0;
    }
    
    public void guardarPdf() {
        this.accionDetalle = 1;
        this.monitorPdfManager.guardar(this.dtoPdf);
        this.dto = this.monitorManager.obtenerPorId(this.dto.getId());
    }
    
    private String obtenerExtension(String nombre) {
        int pto = nombre.indexOf(".");
        return nombre.substring(pto);
    }
    
    public void guardarComentario() {
        
        System.out.println("URL DE LA IMAGEN " + this.urlImagen);
        System.out.println("ADMIN: " + this.esAdmin);
        if (!this.esAdmin) {
            this.commentVDto.setUrlImagen("/img/user.jpg");
        } else {
            this.commentVDto.setUrlImagen(this.urlImagen);
        }        
        this.commentVDto.setVideoDto(this.dtoVideo);
        this.vCommentManager.guardar(this.commentVDto);
        this.commentVDto = this.vCommentManager.inicializar();
        this.dtoVideo = this.monitorVideoManager.obtenerPorId(this.dtoVideo.getId());
    }
    
    /* public void guardarComentarioP() {
        this.commentPDto.setPdfDto(this.dtoPdf);
        this.pCommentManager.guardar(this.commentPDto);
        this.commentPDto = this.pCommentManager.inicializar();
        this.dtoPdf = this.monitorPdfManager.obtenerPorId(this.dtoPdf.getId());
    }
    */
    public String mostrarVideo() {
        return this.monitorManager.getUrlVideo();
    }
    
    public String mostrarPdf() {
        return this.monitorManager.getUrlPdf();
    }
    
    public void eliminarComentario() {
        this.vCommentManager.eliminar(this.idComentarioAEliminar);
        this.idComentarioAEliminar = 0L;
        this.dtoVideo = this.monitorVideoManager.obtenerPorId(this.dtoVideo.getId());        
    }

    public String guardar() {
        this.monitorManager.guardar(dto);
        buscar();
        return this.monitorManager.getUrlLista();
    }

    public MonitorDto getDto() {
        return dto;
    }

    public void setDto(MonitorDto dto) {
        this.dto = dto;
    }

    public MonitorDto getDtoFiltro() {
        return dtoFiltro;
    }

    public void setDtoFiltro(MonitorDto dtoFiltro) {
        this.dtoFiltro = dtoFiltro;
    }

    public List<MonitorDto> getLista() {
        return lista;
    }

    public void setLista(List<MonitorDto> lista) {
        this.lista = lista;
    }

    public MonitorManager getMonitorManager() {
        return monitorManager;
    }

    public void setMonitorManager(MonitorManager monitorManager) {
        this.monitorManager = monitorManager;
    }

    public UploadedFile getUpFile() {
        return upFile;
    }

    public void setUpFile(UploadedFile upFile) {
        this.upFile = upFile;
    }

    public MonitorVideoDto getDtoVideo() {
        return dtoVideo;
    }

    public void setDtoVideo(MonitorVideoDto dtoVideo) {
        this.dtoVideo = dtoVideo;
    }

    public MonitorPdfDto getDtoPdf() {
        return dtoPdf;
    }

    public void setDtoPdf(MonitorPdfDto dtoPdf) {
        this.dtoPdf = dtoPdf;
    }

    public int getAccion() {
        return accion;
    }

    public void setAccion(int accion) {
        this.accion = accion;
    }

    public int getAccionDetalle() {
        return accionDetalle;
    }

    public void setAccionDetalle(int accionDetalle) {
        this.accionDetalle = accionDetalle;
    }

    public int getVerVideo() {
        return verVideo;
    }

    public void setVerVideo(int verVideo) {
        this.verVideo = verVideo;
    }

    public int getVerPdf() {
        return verPdf;
    }

    public void setVerPdf(int verPdf) {
        this.verPdf = verPdf;
    }

    public VideoCommentDto getCommentVDto() {
        return commentVDto;
    }

    public void setCommentVDto(VideoCommentDto commentVDto) {
        this.commentVDto = commentVDto;
    }

    public PdfCommentDto getCommentPDto() {
        return commentPDto;
    }

    public void setCommentPDto(PdfCommentDto commentPDto) {
        this.commentPDto = commentPDto;
    }

    public boolean isEsAdmin() {
        return esAdmin;
    }

    public void setEsAdmin(boolean esAdmin) {
        this.esAdmin = esAdmin;
    }

    public String getUrlImagen() {
        return urlImagen;
    }

    public void setUrlImagen(String urlImagen) {
        this.urlImagen = urlImagen;
    }

    public Long getIdComentarioAEliminar() {
        return idComentarioAEliminar;
    }

    public void setIdComentarioAEliminar(Long idComentarioAEliminar) {
        this.idComentarioAEliminar = idComentarioAEliminar;
    }
}
